<?php
namespace Shop\Model;
use Think\Model;

class ShopActivityModel extends Model {

	protected $tableName = 'shop_activity';
	protected $_validate = array(
		array('title', '1,255', '活动标题长度不对', 1, 'length'),
		array('content','','请填写活动详情',1,'notequal'),
		array('main_img','/[0-9]\d*/','请上传活动主图',Model::MUST_VALIDATE),

	);
	protected $_auto = array(
		array('create_time', NOW_TIME, self::MODEL_INSERT),
	);

	public function activity_list($option){	//获取列表
		if (!empty($option['status'])){
			$where_arr[] = 'status = ' . $option['status'];
		}else{
			$where_arr[] = 'status = 0';
		}
		$where_str = '';
		if (!empty($where_arr)){
			$where_str .=  implode(' and ', $where_arr);
		}
		$ret['list']  = $this->where($where_str)->order('sort desc')->page($option['page'], $option['r'])->select();
		$ret['count'] = $this->where($where_str)->count();
		return $ret;
	}

	public function activity_by_id($id){	//按id获取活动
		$ret = $this->where('id ='.$id)->find();
		return $ret;
	}

	public function edit_activity($data){		//新增或编辑活动
		if (empty($data['id'])){
			$ret = $this->add($data);
		}else{
			$ret = $this->where('id='.$data['id'])->save($data);
		}
		return $ret;
	}

}

